Skip to main content

Topic 4

Topic 4 has 2 parts: Inheritance and Polymorphism.


Topic 04 - Part A - Inheritance

Remember what we learnt about relationships and collaborations. and abstraction

Inheritance plays a role in generalising and specialising parts of code in OOP.

(Child) Classes can inherit attributes from their parent classes (think: Real life) using "is a" relationships. To go back to my shape example, you can say that "A Triangle is a Shape".

Child classes will inherit all features from their respective parent class, but they are also able to override the parent and have unique pieces of their own, just as drawing a circle instead of a rectangle.

Child classes are unable to access their parent's private members (e.g. variables), but can access protected and public members. In C# we can declare inheritance like this:

class ChildClass : ParentClass

Overall, inheritance makes programs more flexible and adaptable, fortifying the scalable ideology.

Topic 04 - Part B - Polymorphism

The concept of polymorphism is that something can have many different forms.

Each form can be a child class (see above) that's used in place of the parent class.

Parent classes need to have placeholder "default" methods that need to get overridden by their child class. This is a must.

Abstract classes cannot create objects, meaning that a parent class behaves differently to a regular one.

This helps with the expandability of a program as we have simple "template" classes in a sense. For example, each of these "Topic x" pages all follow the same format/structure. They could be considered as a "child object" (from a child class) of a parent "Topic" class.

New children can be added without any extra changes.

To be continued...

I'll be updating this page when I can and posting updates on the Discord Server about it.

For now, you may want to update yourself on the core concepts or read some important information about this site.

The above links may be broken at the moment. I'm working to fix this. They work fine on my development server but not the main one for some reason.

If there is a specific section you feel should be added and/or changed, let me know and I'll address it as soon as realistically possible.